package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 山外小王
 * @Description 检查组Service实现类
 * @Date 2024/04/13/16:41
 */
@Service
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("[检查组-新增]data:{},checkitemIds{}",checkGroup,checkitemIds);
        checkGroupDao.insert(checkGroup);
        for (Integer checkitemId : checkitemIds) {
            checkGroupDao.insertCheckGroupAndCheckItem(checkGroup.getId(),checkitemId);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        //分页查询数据库
        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryPageBean.getQueryString());
        //封装分页信息
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.selectAll();
    }

    @Override
    public Result deleteCheckGroupitemById(Long id) {
        //被套餐引⽤的检查组不可删除
        Integer count = checkGroupDao.countCheckGroupById(id);
        if (count > 0) {
            return new Result(false,"被套餐引⽤的检查组不可删除",null);//throw new BusinessRuntimeException("被套餐引⽤的检查组不可删除");
        }
        //删除关联表数据
        checkGroupDao.deleteCheckGroupCheckItemById(Math.toIntExact(id));
        //删除选中数据
        checkGroupDao.deleteById(id);
        return new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS,null);
    }

    @Override
    public Result findCheckItemIdsByCheckGroupId(Long id) {
        Integer[] checkGroupIds = checkGroupDao.selectCheckGroupIdsById(id);
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroupIds);
    }

    /**
     * 根据id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Long id) {
        return checkGroupDao.selectCheckGroupById(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改检查组信息
        checkGroupDao.update(checkGroup);
        //删除关联关系
        checkGroupDao.deleteCheckGroupCheckItemById(checkGroup.getId());
        //插入关联关系
        for (Integer checkitemId : checkitemIds) {
            checkGroupDao.insertCheckGroupAndCheckItem(checkGroup.getId(),checkitemId);
        }
    }

    @Override
    public Integer[] findCheckGroupIdsBySetmealId(Integer id) {
        return checkGroupDao.selectCheckGroupIdssById(id);
    }
}
